package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.utils.ThrowUtils;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.service.INursingPlanService;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-17
 */
@Service
@RequiredArgsConstructor
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {

    private final NursingPlanMapper nursingPlanMapper;
    private final NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        // 1.根据id查询护理计划
        NursingPlan nursingPlan = nursingPlanMapper.selectById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);

        // 2.根据护理计划id查询关联的所有护理项目
        List<NursingProjectPlanVo> list = nursingProjectPlanMapper.selectByPlanId(id);

        nursingPlanVo.setProjectPlans(list);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan(NursingPlan nursingPlan) {
        return save(nursingPlan) ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param requestParam 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto requestParam) {
        try {
            // 属性拷贝
            NursingPlan nursingPlan = new NursingPlan();
            BeanUtils.copyProperties(requestParam, nursingPlan);

            // 判断dto中的项目列表为空，如果不为空，则先删除护理计划与护理项目的关系，然后重新批量添加
            if (CollectionUtils.isNotEmpty(requestParam.getProjectPlans())) {
                // 删除护理计划对应的护理项目列表
                nursingProjectPlanMapper.delete(Wrappers.lambdaQuery(NursingProjectPlan.class)
                        .eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
                // 补全护理计划id
                List<NursingProjectPlan> projectPlans = requestParam.getProjectPlans();
                projectPlans.forEach(nursingProjectPlan -> nursingProjectPlan.setPlanId(requestParam.getId()));
                // 批量保存护理计划对应的护理项目列表
                nursingProjectPlanMapper.batchInsert(projectPlans, nursingPlan.getId());
            }

            // 不管项目列表是否为空，都要修改护理计划
            return nursingPlanMapper.update(nursingPlan, Wrappers.lambdaQuery(NursingPlan.class).eq(NursingPlan::getId, nursingPlan.getId()));
        } catch (BeansException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteNursingPlanById(Long id) {
        // 删除护理计划与护理项目的关系
        nursingProjectPlanMapper.deleteById(id);
        // 删除护理计划
        return removeById(id) ? 1 : 0;
    }

    @Override
    public int insertNursingPlan(NursingPlanDto requestParam) {

        // 检查是否已存在同名计划
        LambdaQueryWrapper<NursingPlan> queryWrapper = new LambdaQueryWrapper<>(NursingPlan.class)
                .eq(NursingPlan::getPlanName, requestParam.getPlanName());
        ThrowUtils.throwIf(this.getOne(queryWrapper) != null, "护理计划名称已存在: " + requestParam.getPlanName());

        // 属性拷贝
        NursingPlan nursingPlan = NursingPlan.builder().planName(requestParam.getPlanName()).sortNo(requestParam.getSortNo()).build();
        baseMapper.insert(nursingPlan);

        // 批量保存护理项目计划关系
        int count = nursingProjectPlanMapper.batchInsert(requestParam.getProjectPlans(), nursingPlan.getId());
        return count == 0 ? 0 : 1;
    }
}
